import { useState, useEffect } from "react";
import useBasePage from "./useBasePage";
import { isEmptyArray } from "@/utils";
import { movieServiceApi } from "@/api";
import type { MovieModuleType } from "@/types/movieTag";

export default function useQueryMovieList(type: MovieModuleType, deps = []) {
  const [loading, setLoading] = useState(false);
  const {
    total,
    setTotal,
    pageNum,
    setPageNum,
    pageSize,
    setPageSize,
    tableData: movies,
    setTableData: setMovies,
    tableReFresh,
    refreshTableById,
    resetPageData,
  } = useBasePage();
  // 实际项目中，这里应该从API获取电影数据
  const fetchMovies = async () => {
    try {
      setLoading(true);
      const result = await movieServiceApi.getMovieList({
        type,
        pageNum,
        pageSize,
      });
      if (result.code === 200 && !isEmptyArray(result?.data?.list)) {
        setMovies(result.data.list);
        setTotal(result.data.total);
      }
    } catch (error) {
      console.error("获取电影列表失败:", error);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    console.log("type:", type, "pageNum:", pageNum, "pageSize:", pageSize);
    fetchMovies();
  }, [type, pageNum, pageSize]);

  return {
    total,
    setTotal,
    pageNum,
    setPageNum,
    pageSize,
    setPageSize,
    tableReFresh,
    refreshTableById,
    resetPageData,
    movies,
    setMovies,
    loading,
    setLoading,
  };
}
